import os

from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext import db
from google.appengine.api import images

from ixapi.db import FilesDB

class FileView (webapp.RequestHandler):
  def get(self):
    FileDb = db.get(self.request.get("id"))
    if FileDb.file:
      if self.request.get("type")=="thumb":
        if FileDb.type.find("image") != -1:
          self.response.headers['Content-Type'] = "image/png"
          self.response.out.write(images.resize(FileDb.file, 64, 64))
        else:
          self.redirect("/assets/images/file-icon-64.png")
      else:
        self.response.headers['Content-Type'] = FileDb.type
        self.response.out.write(FileDb.file)
    else:
      self.error(404)

def main():
  application = webapp.WSGIApplication([('/file', FileView)],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()
